package com.pheruagor.handmanager.storage.impl.sqlite {
    import com.pheruagor.handmanager.model.business.Member;
    import com.pheruagor.handmanager.model.business.activity.PlayerActivity;
    import com.pheruagor.handmanager.storage.api.PlayerActivityStorage;

    import flash.data.SQLResult;
    import flash.data.SQLStatement;
    import flash.errors.SQLError;
    import flash.events.SQLEvent;

    /**
     * Manage the storage of player activity
     * Used to load/save/remove element related to the player activities
     * @see PlayerActivityStorage
     */
    public class PlayerActivitySQLiteStorage implements PlayerActivityStorage {
        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        private var m_table:Table;
        private var m_storage:SQLiteStorage;

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------

        public function PlayerActivitySQLiteStorage() {
            createTable();
            m_storage = new SQLiteStorage();
        }

        private function get table():Table {
            return m_table;
        }

        private function createTable():void {
            m_table = new Table('playerActivity');
            m_table.creationStatement = "( id TEXT PRIMARY KEY,"
                + " memberId TEXT CONSTRAINT fk_member_id REFERENCES member(id), beginDate TEXT,"
                + " endDate TEXT, description TEXT, isLeftWingman INTEGER(1), isRightWingman INTEGER(1),"
                + " isLeftBackcourt INTEGER(1), isCenterBackcourt INTEGER(1), isRightBackcourt INTEGER(1),"
                + " isPivot INTEGER(1), isGuardian INTEGER(1))";
        }

        public function saveActivity(activity:PlayerActivity, onResult:Function = null, onFault:Function = null):void {
            if (activity == null) {
                throw new ArgumentError("Activity cannot be null");
            }
            if (activity.id == null) {
                throw new ArgumentError("Activity id cannot be null");
            }
            if (activity.memberId == null || activity.memberId.length == 0) {
                throw new ArgumentError("Member id cannot be null or empty");
            }

            var beginDate:String = activity.beginDate == null ? null : activity.beginDate.toString();
            var endDate:String = activity.endDate == null ? null : activity.endDate.toString();

            table.insertStatement = "(id, memberId, beginDate, endDate, description, isLeftWingman, isRightWingman, isLeftBackcourt,"
                + " isCenterBackcourt, isRightBackcourt, isPivot, isGuardian) values('" + activity.id + "','" + activity.memberId + "','"
                + beginDate + "','" + endDate + "','" + activity.description + "'," + int(activity.isLeftWingman) + ","
                + int(activity.isRightWingman) + "," + int(activity.isLeftBackcourt) + "," + int(activity.isCenterBackcourt) + ","
                + int(activity.isRightBackcourt) + "," + int(activity.isPivot) + "," + int(activity.isGuardian) + ");";
            var resultHandler:Function = function(event:SQLEvent):void {
                    if (onResult != null) {
                        onResult(event);
                    }
                }
            var faultHandler:Function = function():void {
                    if (onFault != null) {
                        onFault();
                    }
                }
            m_storage.save(table, resultHandler, faultHandler);
        }

        public function removeActivity(activityId:String, onResult:Function = null, onFault:Function = null):void {
            if (activityId == null || activityId.length == 0) {
                throw new ArgumentError("activityId cannot be null or empty");
            }

            var resultHandler:Function = function(event:SQLEvent):void {
                    if (onResult != null) {
                        onResult();
                    }
                }
            var faultHandler:Function = function(event:SQLError):void {
                    if (onFault != null) {
                        onFault();
                    }
                }
            table.removeStatement = "WHERE id='" + activityId + "';";
            m_storage.remove(table, resultHandler, faultHandler);
        }

        public function listActivities(member:Member, onResult:Function, onFault:Function = null):void {
            var resultHandler:Function = function(event:SQLEvent):void {
                    if (onResult != null) {
                        var sqlResult:SQLResult = SQLStatement(event.currentTarget).getResult();
                        var localResults:Array = sqlResult.data;
                        var results:Array = new Array();
                        for each (var item:Object in localResults) {
                            var activity:PlayerActivity = PlayerActivity.fromObject(item);
                            results.push(activity);
                        }
                        onResult(results);
                    }
                }
            var faultHandler:Function = function():void {
                    if (onFault != null) {
                        onFault();
                    }
                }

            table.selectStatement = "* FROM " + table.name + " WHERE memberId='" + member.id + "';";
            m_storage.load(table, resultHandler, faultHandler);
        }

        public function removeAllActivitiesFromMember(member:Member, onResult:Function = null, onFault:Function = null):void {
            table.removeStatement = " WHERE memberId='" + member.id + "';";
            var resultHandler:Function = function(event:SQLEvent):void {
                    if (onResult != null) {
                        onResult();
                    }
                }
            var faultHandler:Function = function():void {
                    if (onFault != null) {
                        onFault();
                    }
                }
            m_storage.remove(table, resultHandler, faultHandler);
        }

        public function removeAllActivities(onResult:Function = null, onFault:Function = null):void {
            table.removeStatement = ";";
            var resultHandler:Function = function(event:SQLEvent):void {
                    if (onResult != null) {
                        onResult();
                    }
                }
            var faultHandler:Function = function():void {
                    if (onFault != null) {
                        onFault();
                    }
                }
            m_storage.remove(table, resultHandler, faultHandler);
        }
    }

}